package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.shop.Product;
import com.mvp.shq.washer.entity.shop.result.MyOrderListRestult;
import com.mvp.shq.washer.entity.shop.result.OrderDataListResult;
import com.mvp.shq.washer.entity.shop.result.OrderDetailListRestult;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.StringUtils;
import com.mvp.shq.washer.view.widget.CustomDialog;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by westing on 2017/10/11.
 * 我的多商品订单列表
 */

public class OrderMyAllListAdapter extends RecyclerView.Adapter<OrderMyAllListAdapter.ViewHolder> {

    private Context context;
    private List<OrderDataListResult> myOrderListRestultList;
    private MyAllOrderItemOnClick myAllOrderItemOnClick;
    private CustomDialog mDialog;
    public OrderMyAllListAdapter(Context mContext, List<OrderDataListResult> myOrderListRestults,MyAllOrderItemOnClick myAllOrderItemOnClick) {
        context = mContext;
        this.myOrderListRestultList = myOrderListRestults;
        this.myAllOrderItemOnClick=myAllOrderItemOnClick;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ordermyall, parent, false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.recyclerview.setLayoutManager(new LinearLayoutManager(context));
        RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.HORIZONTAL, 10, ContextCompat.getColor(context, R.color.white));
        holder.recyclerview.addItemDecoration(divider);

        if(myOrderListRestultList==null)return;
        OrderDataListResult myOrderListRestult=myOrderListRestultList.get(position);
        if(myOrderListRestult==null)return;
        List<OrderDetailListRestult> detailListRestultList=myOrderListRestult.getEcOrderDetailList();
        if(detailListRestultList==null)return;
        OrderMyListAdapter listadapter = new OrderMyListAdapter(context, detailListRestultList);
        holder.recyclerview.setAdapter(listadapter);

        holder.bntTake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myAllOrderItemOnClick.onBtnClick(position);
            }
        });
        holder.linItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myAllOrderItemOnClick.onItemOnClick(position);
            }
        });
        double money1 = myOrderListRestult.getCashPrice();
        String money2 = StringUtils.BigDecimalToStr(BigDecimal.valueOf(money1));
        holder.tvMoney1.setText((int)money1+"");
        holder.tvMoney2.setText(money2);

        double integral = myOrderListRestult.getIntegralPrice();
        holder.tvIntegral.setText((int)integral+"");

        holder.llMoney.setVisibility(View.VISIBLE);
        holder.tvAdd.setVisibility(View.VISIBLE);
        holder.tvIntegral.setVisibility(View.VISIBLE);
        holder.tvJifen.setVisibility(View.VISIBLE);
        holder.tvAdd.setVisibility(View.VISIBLE);

        if (money1==0 && money2.equals(0)) {
            holder.tvAdd.setVisibility(View.GONE);
            holder.tvMoney1.setVisibility(View.GONE);
            holder.tvMoney2.setVisibility(View.GONE);
        }
        if (integral==0) {
            holder.tvIntegral.setVisibility(View.GONE);
            holder.tvJifen.setVisibility(View.GONE);
            holder.tvAdd.setVisibility(View.GONE);
        }

        holder.tvOrderId.setText("订单编号："+myOrderListRestult.getOrderNo());
        switch (myOrderListRestult.getOrderStatus()){
            case DataDictionary.ORDERNOPAY://待付款
                holder.bntTake.setText("付款");
                holder.tvStatau.setText("待付款");
                break;
            case DataDictionary.ORDERTAKE://待收货
                holder.bntTake.setText("确认收货");
                holder.tvStatau.setText("待收货");
                break;
            case DataDictionary.ORDEREVALUATE://待评价
                holder.bntTake.setText("评价");
                holder.tvStatau.setText("待评价");
                break;
            case DataDictionary.ORDERNCOMPLETED://完成
                holder.bntTake.setText("完成");
                holder.tvStatau.setText("已完成");
                break;
            case DataDictionary.ORDERTIMEOUT://订单超时
                holder.bntTake.setText("已超时");
                holder.tvStatau.setText("已超时");
                break;
            case DataDictionary.ORDEREWAITREFUND://退款中
                holder.bntTake.setText("退款");
                holder.tvStatau.setText("退款中");
                break;
            case DataDictionary.ORDEREREFUNDED://已退款
                holder.bntTake.setText("退款");
                holder.tvStatau.setText("已退款");
                break;
        }


    }


    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }


    @Override
    public int getItemCount() {
        return myOrderListRestultList!=null?myOrderListRestultList.size():0;
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.recyclerview)
        RecyclerView recyclerview;
        @BindView(R.id.tv_money_1)
        TextView tvMoney1;//实付元
        @BindView(R.id.tv_money2)
        TextView tvMoney2;//实付角分
        @BindView(R.id.ll_money)
        LinearLayout llMoney;//实付金额
        @BindView(R.id.tv_add)
        TextView tvAdd;//加号
        @BindView(R.id.tv_integral)
        TextView tvIntegral;//积分
        @BindView(R.id.ll_integral)
        LinearLayout llIntegral;//实付积分
        @BindView(R.id.bnt_take)
        TextView bntTake;//付款、确认收货、评价
        @BindView(R.id.tv_orderid)
        TextView tvOrderId;//订单id
        @BindView(R.id.tv_statau)
        TextView tvStatau;//订单状态
        @BindView(R.id.tv_ji_fen)
        TextView tvJifen;
        @BindView(R.id.lin_item)
        LinearLayout linItem;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public interface MyAllOrderItemOnClick{
        void onItemOnClick(int position);
        void onBtnClick(int position);
    }
}
